#![allow(unused_imports,dead_code,unused_assignments)]
use axum::{
    extract::{Form, Query, Json, Multipart,DefaultBodyLimit},
    routing::{get,post},
    response::Response,
    body::{Body, Bytes},
    Router
};
use chrono::{NaiveDate,NaiveDateTime};
use crate::domain::CustOrderBean;
use crate::domain::CustOrderQueryRequest;
use crate::domain::my_date_format::DATE_FORMAT;
use crate::domain::my_date_time_format::DATE_TIME_FORMAT;
use crate::serviceimpl::goods_type_service::list_active_goods_types as service_list_active_goods_types;
use crate::serviceimpl::goods_type_service::find_goods_type_by_id as service_find_goods_type_by_id;
use crate::serviceimpl::goods_type_service::update_goods_type as service_update_goods_type;
use crate::utils::excel_util::export_excel_workbook_with_image;
use crate::utils::pdf_util::export_pdf_with_images;
use serde::{Deserialize};
use serde_json::{Value,Map};
use std::fs;
use tower_http::{trace::TraceLayer};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GoodsTypeIdCustRequest {
	pub id: i64,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GoodsTypeCustRequest {
	pub id: i64,
	pub name:String,
}

pub fn admin_goods_type_cust_controller() -> Router {
	Router::new()
	.route("/goods_type_list", post(admin_goods_type_list))
	.route("/show_edit_type", post(show_edit_type))
	.route("/updateGoodsType", post(update_goods_type))
	.layer(DefaultBodyLimit::max(2*1024*1024))
	.layer(TraceLayer::new_for_http())
}

pub async fn admin_goods_type_list() ->String {
	let list = service_list_active_goods_types().await;

	match list {
		Err(_) => {
			println!("Error!");
			r#"{  "rows": null,  "success": true}"#.to_string()
		},
		Ok(list) => {
			let json = serde_json::to_string_pretty(&list).unwrap();
			//println!("{}", json);
			let mut map = Map::new();
			map.insert("success".to_string(), Value::from(true));
			map.insert(
				"rows".to_string(),
				serde_json::from_str(&json).unwrap(),
			);

			let resultjson = serde_json::to_string_pretty(&map).unwrap();
			//println!("{}", resultjson);
			return resultjson;
		}
	}
}

pub async fn show_edit_type(Json(goods_type_id_cust_request):Json<GoodsTypeIdCustRequest>) ->String {
    let id:i64 = goods_type_id_cust_request.id;
	let goods_type = service_find_goods_type_by_id(id).await;
	match goods_type {
		Err(_) => {
			println!("Error!");
			r#"{  "rows": null,  "success": true}"#.to_string()
		},
		Ok(goods_type_value) => {
			let json = serde_json::to_string_pretty(&goods_type_value).unwrap();
			//println!("{}", json);

			let mut map = Map::new();
			map.insert("success".to_string(), Value::from(true));
			map.insert(
				"rows".to_string(),
				serde_json::from_str(&json).unwrap(),
			);

			let resultjson = serde_json::to_string_pretty(&map).unwrap();
			//println!("{}", resultjson);
			return resultjson;
		}
	}
}

pub async fn update_goods_type(Json(goods_type_cust_request):Json<GoodsTypeCustRequest>) ->String {
    let id:i64 = goods_type_cust_request.id;
    let name:String = goods_type_cust_request.name;
	let mut goods_type = service_find_goods_type_by_id(id).await.unwrap();
	goods_type.name = name;
	let _ = service_update_goods_type(goods_type).await.unwrap();
	r#"{  "rows": null,  "success": true}"#.to_string()
}
